package com.lpj;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class CallSumMapper extends Mapper<LongWritable, Text, Text, CallBean> {
    //拿到日志中的一行数据，切分各个字段，抽取出我们需要的字段：手机号，上行流量，下行流量
    //封装成key-value发送出去
    @Override
    protected void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {
        //拿到一行数据
        String line = value.toString();
        //切分成各个字段
        String[] fields = StringUtils.split(line, "  ");
        //拿到手机号的字段
        String phoneNumber = fields[0];
        //拿到打出去电话的字段
        long call_out = Long.parseLong(fields[2]);
        //拿到打进来电话的字段
        long call_in   = Long.parseLong(fields[3]);
        //最后一步，封装数据为key-value进行输出
        context.write(new Text(phoneNumber), new CallBean(phoneNumber, call_out, call_in));

    }
}
